//
//  FoundServer.m
//  Bees
//
//  Created by NowYouCanSee on 2019/2/26.
//  Copyright © 2019 xiaohan. All rights reserved.
//

#import "FoundServer.h"

@implementation FoundServer
- (void)getDiscoverComplete:(void(^)(BOOL success,NSString*msg))complete{
    [NetworkManager GET:URL_bees_get_discover parameters:@{} responseCache:^(id responseCache) {
        XHBaseModel*base = [XHBaseModel modelWithJSON:responseCache];
        if ([base.code isEqualToNumber:@200]) {
            self.foundModel = [FoundModel modelWithJSON:base.data];
            NSMutableArray*categoryTitles = [NSMutableArray array];
            [self.foundModel.category enumerateObjectsUsingBlock:^(FoundCategoryModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [categoryTitles addObject:obj.name];
            }];
            self.foundModel.categoryTitles = categoryTitles.copy;
            !complete?:complete(YES,base.message);
        }else{
            !complete?:complete(NO,base.message);
        }
    } success:^(id responseObject) {
        XHBaseModel*base = [XHBaseModel modelWithJSON:responseObject];
        if ([base.code isEqualToNumber:@200]) {
            self.foundModel = [FoundModel modelWithJSON:base.data];
            NSMutableArray*categoryTitles = [NSMutableArray array];
            [self.foundModel.category enumerateObjectsUsingBlock:^(FoundCategoryModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [categoryTitles addObject:obj.name];
            }];
            self.foundModel.categoryTitles = categoryTitles.copy;
            !complete?:complete(YES,base.message);
        }else{
            !complete?:complete(NO,base.message);
        }
    } failure:^(NSError *error) {
        !complete?:complete(NO,kNetworkError);
    }];
}

- (void)getCategroyListIsMore:(BOOL)isMore completion:(void(^)(BOOL success, BOOL isHaveNextPage,NSString *msg))completion{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"id"] = VALIDATE_STRING(self.categroyId);
    NSInteger page = isMore ? (self.cmtPage + 1) : 1;
    params[@"page"]=@(page);
    self.latestParams = params;
    [NetworkManager GET:[URL_discover_more stringByAppendingPathComponent:self.categroyId] parameters:params success:^(id responseObject) {
        XHBaseModel *base = [XHBaseModel modelWithJSON:responseObject];
        if ([base.code isEqualToNumber:@200]) {
            XHBasePagingModel *pageM = [XHBasePagingModel modelWithJSON:base.data];
            // 用户上拉后有快速下拉, 下拉的数据先回来, 上拉的数据后回来
            if (self.latestParams != params) {
                return;
            }
            //记录页码
            self.cmtPage = page;
            if (!isMore) {
                [self.categroyList removeAllObjects];
            }
            [self.categroyList addObjectsFromArray:[NSArray modelArrayWithClass:[FoundRecommendModel class] json:pageM.data]];
            !completion?:completion(YES,pageM.isHaveNextPage,base.message);
        }else{
            !completion?:completion(NO,NO,base.message);
        }
    } failure:^(NSError *error) {
        !completion?:completion(NO,NO,kNetworkError);
    }];
}

- (void)getCenterListIsMore:(BOOL)isMore completion:(void(^)(BOOL success, BOOL isHaveNextPage,NSString *msg))completion{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"key"] = VALIDATE_STRING(self.key);
    NSInteger page = isMore ? (self.cmtPage + 1) : 1;
    params[@"page"]=@(page);
    self.latestParams = params;
    [NetworkManager GET:URL_game_center parameters:params success:^(id responseObject) {
        XHBaseModel *base = [XHBaseModel modelWithJSON:responseObject];
        if ([base.code isEqualToNumber:@200]) {
            XHBasePagingModel *pageM = [XHBasePagingModel modelWithJSON:base.data[@"all"]];
            // 用户上拉后有快速下拉, 下拉的数据先回来, 上拉的数据后回来
            if (self.latestParams != params) {
                return;
            }
            //记录页码
            self.cmtPage = page;
            if (!isMore) {
                [self.gamesList removeAllObjects];
            }
            self.recommendedList = [NSArray modelArrayWithClass:[FoundRecommendModel class] json:[base.data objectForKey:@"recommend"]];
            [self.gamesList addObjectsFromArray:[NSArray modelArrayWithClass:[FoundRecommendModel class] json:pageM.data]];
            !completion?:completion(YES,pageM.isHaveNextPage,base.message);
        }else{
            !completion?:completion(NO,NO,base.message);
        }
    } failure:^(NSError *error) {
        !completion?:completion(NO,NO,kNetworkError);
    }];
}

- (void)getNoticeListIsMore:(BOOL)isMore completion:(void(^)(BOOL success, BOOL isHaveNextPage,NSString *msg))completion{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSInteger page = isMore ? (self.cmtPage + 1) : 1;
    params[@"page"]=@(page);
    self.latestParams = params;
    [NetworkManager GET:URL_game_notice parameters:params success:^(id responseObject) {
        XHBaseModel *base = [XHBaseModel modelWithJSON:responseObject];
        if ([base.code isEqualToNumber:@200]) {
            XHBasePagingModel *pageM = [XHBasePagingModel modelWithJSON:base.data];
            // 用户上拉后有快速下拉, 下拉的数据先回来, 上拉的数据后回来
            if (self.latestParams != params) {
                return;
            }
            //记录页码
            self.cmtPage = page;
            if (!isMore) {
                [self.noticeList removeAllObjects];
            }
            NSArray <InformationModel*>*informationModels = [NSArray modelArrayWithClass:[InformationModel class] json:pageM.data];
            [informationModels enumerateObjectsUsingBlock:^(InformationModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                CGFloat contentH = 0.f;
                contentH += [obj.title heightForFont:kDefaultFont(15) width:kScreenWidth-AdaptedWidth(64)];
                contentH += [obj.content heightForFont:kDefaultFont(12) width:kScreenWidth-AdaptedWidth(64)];
                contentH += obj.cover.length?AdaptedWidth(170):AdaptedWidth(150);///固定间距
                obj.contentH = contentH;
            }];
            [self.noticeList addObjectsFromArray:informationModels];
            !completion?:completion(YES,pageM.isHaveNextPage,base.message);
        }else{
            !completion?:completion(NO,NO,base.message);
        }
    } failure:^(NSError *error) {
        !completion?:completion(NO,NO,kNetworkError);
    }];
}

- (void)getInformationListIsMore:(BOOL)isMore completion:(void(^)(BOOL success, BOOL isHaveNextPage,NSString *msg))completion{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSInteger page = isMore ? (self.cmtPage + 1) : 1;
    params[@"page"]=@(page);
    self.latestParams = params;
    [NetworkManager GET:URL_game_information parameters:params success:^(id responseObject) {
        XHBaseModel *base = [XHBaseModel modelWithJSON:responseObject];
        if ([base.code isEqualToNumber:@200]) {
            XHBasePagingModel *pageM = [XHBasePagingModel modelWithJSON:base.data];
            // 用户上拉后有快速下拉, 下拉的数据先回来, 上拉的数据后回来
            if (self.latestParams != params) {
                return;
            }
            //记录页码
            self.cmtPage = page;
            if (!isMore) {
                [self.informationList removeAllObjects];
            }
            NSArray <InformationModel*>*informationModels = [NSArray modelArrayWithClass:[InformationModel class] json:pageM.data];
            [informationModels enumerateObjectsUsingBlock:^(InformationModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                CGFloat contentH = 0.f;
                contentH += [obj.title heightForFont:kDefaultFont(15) width:kScreenWidth-AdaptedWidth(64)];
                contentH += [obj.content heightForFont:kDefaultFont(12) width:kScreenWidth-AdaptedWidth(64)];
                contentH += obj.cover.length?AdaptedWidth(170):AdaptedWidth(150);///固定间距
                obj.contentH = contentH;
            }];
            [self.informationList addObjectsFromArray:informationModels];
            !completion?:completion(YES,pageM.isHaveNextPage,base.message);
        }else{
            !completion?:completion(NO,NO,base.message);
        }
    } failure:^(NSError *error) {
        !completion?:completion(NO,NO,kNetworkError);
    }];
}

- (void)getEvaluationListIsMore:(BOOL)isMore completion:(void(^)(BOOL success, BOOL isHaveNextPage,NSString *msg))completion{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSInteger page = isMore ? (self.cmtPage + 1) : 1;
    params[@"page"]=@(page);
    self.latestParams = params;
    [NetworkManager GET:URL_game_evaluation parameters:params success:^(id responseObject) {
        XHBaseModel *base = [XHBaseModel modelWithJSON:responseObject];
        if ([base.code isEqualToNumber:@200]) {
            XHBasePagingModel *pageM = [XHBasePagingModel modelWithJSON:base.data];
            // 用户上拉后有快速下拉, 下拉的数据先回来, 上拉的数据后回来
            if (self.latestParams != params) {
                return;
            }
            //记录页码
            self.cmtPage = page;
            if (!isMore) {
                [self.evaluationList removeAllObjects];
            }
            NSArray <EvaluationModel*>*evaluationModels = [NSArray modelArrayWithClass:[EvaluationModel class] json:pageM.data];
            [evaluationModels enumerateObjectsUsingBlock:^(EvaluationModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                CGFloat contentH = 0.f;
                if (obj.big_cover.length && obj.is_top) {
                    contentH += [obj.title heightForFont:kDefaultFont(15) width:kScreenWidth-AdaptedWidth(64)];
                    contentH += AdaptedWidth(220);
                }else{
                    CGFloat titleH = [obj.title heightForFont:kDefaultFont(15) width:kScreenWidth-AdaptedWidth(140)];
                    contentH += titleH>AdaptedWidth(70)?titleH:AdaptedWidth(70);
                    contentH +=AdaptedWidth(100);
                }
                obj.contentH = contentH;
            }];
            [self.evaluationList addObjectsFromArray:evaluationModels];
            !completion?:completion(YES,pageM.isHaveNextPage,base.message);
        }else{
            !completion?:completion(NO,NO,base.message);
        }
    } failure:^(NSError *error) {
        !completion?:completion(NO,NO,kNetworkError);
    }];
}

- (void)getCollectionListCompletion:(void(^)(BOOL success,NSString *msg))completion{
    [NetworkManager GET:URL_mine_dapp_collect parameters:@{} responseCache:^(id responseCache) {
        XHBaseModel*base = [XHBaseModel modelWithJSON:responseCache];
        if ([base.code isEqualToNumber:@200]) {
            self.collectionList = [NSArray modelArrayWithClass:FoundRecommendModel.class json:base.data];
            !completion?:completion(YES,base.message);
        }else{
            !completion?:completion(NO,base.message);
        }
    } success:^(id responseObject) {
        XHBaseModel*base = [XHBaseModel modelWithJSON:responseObject];
        if ([base.code isEqualToNumber:@200]) {
            self.collectionList = [NSArray modelArrayWithClass:FoundRecommendModel.class json:base.data];
            !completion?:completion(YES,base.message);
        }else{
            !completion?:completion(NO,base.message);
        }
    } failure:^(NSError *error) {
        !completion?:completion(NO,kNetworkError);
    }];
}
- (void)isCollectionCompletion:(void(^)(BOOL success,NSString *msg))completion{
    if (!self.collectionId) {
        return;
    }
    [NetworkManager GET:[URL_mine_dapp_collect stringByAppendingPathComponent:self.collectionId] parameters:@{@"id":self.collectionId} responseCache:nil success:^(id responseObject) {
        XHBaseModel*base = [XHBaseModel modelWithJSON:responseObject];
        if ([base.code isEqualToNumber:@200]) {
            self.isCol = [base.data integerValue]>=1;
            !completion?:completion(YES,base.message);
        }else{
            !completion?:completion(NO,base.message);
        }
    } failure:^(NSError *error) {
        !completion?:completion(NO,kNetworkError);
    }];
}
- (void)postCollectionListCompletion:(void(^)(BOOL success,NSString *msg))completion{
    [NetworkManager POST:URL_mine_dapp_collect parameters:@{@"id":VALIDATE_STRING(self.collectionId)}  responseCache:nil success:^(id responseObject) {
        XHBaseModel*base = [XHBaseModel modelWithJSON:responseObject];
        if ([base.code isEqualToNumber:@200]) {
            !completion?:completion(YES,base.message);
        }else{
            !completion?:completion(NO,base.message);
        }
    } failure:^(NSError *error) {
        !completion?:completion(NO,kNetworkError);
    }];
}
- (void)putCollectionListCompletion:(void(^)(BOOL success,NSString *msg))completion{
    [NetworkManager PUT:URL_mine_dapp_collect parameters:@{@"id":VALIDATE_STRING(self.collectionId)} success:^(id responseObject) {
        XHBaseModel*base = [XHBaseModel modelWithJSON:responseObject];
        if ([base.code isEqualToNumber:@200]) {
            !completion?:completion(YES,base.message);
        }else{
            !completion?:completion(NO,base.message);
        }
    } failure:^(NSError *error) {
        !completion?:completion(NO,kNetworkError);
    }];
}

- (void)getDappListIsMore:(BOOL)isMore completion:(void(^)(BOOL success, BOOL isHaveNextPage,NSString *msg))completion{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSInteger page = isMore ? (self.cmtPage + 1) : 1;
    params[@"page"]=@(page);
    params[@"key"]=VALIDATE_STRING(self.dappKey);
    self.latestParams = params;
    [NetworkManager GET:URL_dapp_search parameters:params success:^(id responseObject) {
        XHBaseModel *base = [XHBaseModel modelWithJSON:responseObject];
        if ([base.code isEqualToNumber:@200]) {
            XHBasePagingModel *pageM = [XHBasePagingModel modelWithJSON:base.data];
            // 用户上拉后有快速下拉, 下拉的数据先回来, 上拉的数据后回来
            if (self.latestParams != params) {
                return;
            }
            //记录页码
            self.cmtPage = page;
            if (!isMore) {
                [self.dappList removeAllObjects];
            }
            [self.dappList addObjectsFromArray:[NSArray modelArrayWithClass:[FoundRecommendModel class] json:pageM.data]];
            !completion?:completion(YES,pageM.isHaveNextPage,base.message);
        }else{
            !completion?:completion(NO,NO,base.message);
        }
    } failure:^(NSError *error) {
        !completion?:completion(NO,NO,kNetworkError);
    }];
}
- (void)getHotSearchDappListCompletion:(void(^)(BOOL success,NSString *msg))completion{
    [NetworkManager GET:URL_dapp_hot_search parameters:@{} responseCache:^(id responseCache) {
        XHBaseModel*base = [XHBaseModel modelWithJSON:responseCache];
        if ([base.code isEqualToNumber:@200]) {
            self.hotSearchList = [NSArray modelArrayWithClass:FoundRecommendModel.class json:base.data];
            !completion?:completion(YES,base.message);
        }else{
            !completion?:completion(NO,base.message);
        }
    } success:^(id responseObject) {
        XHBaseModel*base = [XHBaseModel modelWithJSON:responseObject];
        if ([base.code isEqualToNumber:@200]) {
            self.hotSearchList = [NSArray modelArrayWithClass:FoundRecommendModel.class json:base.data];
            !completion?:completion(YES,base.message);
        }else{
            !completion?:completion(NO,base.message);
        }
    } failure:^(NSError *error) {
        !completion?:completion(NO,kNetworkError);
    }];
}

- (void)postHotSearchDappId:(NSString*)dappId completion:(void(^)(BOOL success,NSString *msg))completion{
    [NetworkManager POST:URL_dapp_hot_search parameters:@{@"id":VALIDATE_STRING(dappId)} success:^(id responseObject) {
        XHBaseModel*base = [XHBaseModel modelWithJSON:responseObject];
        if ([base.code isEqualToNumber:@200]) {
            !completion?:completion(YES,base.message);
        }else{
            !completion?:completion(NO,base.message);
        }
    } failure:^(NSError *error) {
        !completion?:completion(NO,kNetworkError);
    }];
}
- (void)getTransactionDappsCompletion:(void(^)(BOOL success,NSString *msg))completion{
    [NetworkManager GET:URL_transaction_dapps parameters:@{} success:^(id responseObject) {
        XHBaseModel*base = [XHBaseModel modelWithJSON:responseObject];
        if ([base.code isEqualToNumber:@200]) {
            self.transactionList = [NSArray modelArrayWithClass:FoundRecommendModel.class json:base.data];
            !completion?:completion(YES,base.message);
        }else{
            !completion?:completion(NO,base.message);
        }
    } failure:^(NSError *error) {
        !completion?:completion(NO,kNetworkError);
    }];
}
- (void)getLeaseDappsCompletion:(void(^)(BOOL success,NSString *msg))completion{
    [NetworkManager GET:URL_lease_dapps parameters:@{} success:^(id responseObject) {
        XHBaseModel*base = [XHBaseModel modelWithJSON:responseObject];
        if ([base.code isEqualToNumber:@200]) {
            self.leaseList = [NSArray modelArrayWithClass:FoundRecommendModel.class json:base.data];
            !completion?:completion(YES,base.message);
        }else{
            !completion?:completion(NO,base.message);
        }
    } failure:^(NSError *error) {
        !completion?:completion(NO,kNetworkError);
    }];
}

- (NSMutableArray<FoundRecommendModel*>*)categroyList{
    if (!_categroyList) {
        _categroyList = [NSMutableArray array];
    }
    return _categroyList;
}
- (NSMutableArray<FoundRecommendModel*>*)gamesList{
    if (!_gamesList) {
        _gamesList = [NSMutableArray array];
    }
    return _gamesList;
}
- (NSMutableArray<FoundRecommendModel*>*)dappList{
    if (!_dappList) {
        _dappList = [NSMutableArray array];
    }
    return _dappList;
}
- (NSMutableArray<EvaluationModel*>*)evaluationList{
    if (!_evaluationList) {
        _evaluationList = [NSMutableArray array];
    }
    return _evaluationList;
}
- (NSMutableArray<InformationModel*>*)informationList{
    if (!_informationList) {
        _informationList = [NSMutableArray array];
    }
    return _informationList;
}
- (NSMutableArray<InformationModel*>*)noticeList{
    if (!_noticeList) {
        _noticeList = [NSMutableArray array];
    }
    return _noticeList;
}

@end
